<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    //1.设计这样的一个函数
    function execCode(counter) {
      const promise = new Promise((resolve, reject) => {
        //异步任务
        setTimeout(() => {
          if (counter > 0) {//counter可以计算的情况
            let total = 0
            for (let i = 0; i < counter; i++) {
              total += i
            }
            resolve(total)




          } else {//失败的回调
            reject(`${counter}有问题`)

          }

        }, 3000)
      })

      return promise
    }

    const promise = execCode(100)
    promise.then((value) => {
      console.log('成功有了结果：', value)

    })
    promise.catch((err) => {
      console.log("失败有了结果", err)

    })
    const promise2 = execCode(-100)
    promise2.then(value => {
      console.log('成功:', value)
    })
    promise2.catch(err => {
      console.log("失败：", err)
    })
    //执行一次
    execCode(255).then(value=>{
      console.log("成功：",value)
    }).catch(err =>{
      console.log("失败：",err)
    })
  </script>
</body>
</html>